package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.enums.PlatformStatusEnum;
import com.fulihui.wgj.dal.mybatis.domain.WxPlatform;
import com.fulihui.wgj.dal.mybatis.mapper.WxPlatformMapper;
import com.fulihui.wgj.facade.dto.WxPlatformDTO;
import com.fulihui.wgj.facade.request.platform.WxPlatformLoginPageRequest;
import com.fulihui.wgj.facade.request.platform.WxPlatformPageRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author wang_wx
 * @date 2018-06-20
 */
@Component
public class WxPlatformRepository extends BaseRepository<WxPlatform> {

    @Resource
    WxPlatformMapper wxPlatformMapper;

    /**
     * 查询微信公众号信息
     *
     * @param appId 公众号appId
     * @return
     */
    public WxPlatform queryOneByAppId(String appId) {
        WxPlatform wxPlatform = new WxPlatform();
        wxPlatform.setAppId(appId);
        WxPlatform selectOne = wxPlatformMapper.selectOne(wxPlatform);
        return selectOne;
    }

    /**
     * 新增公众号
     *
     * @param wxPlatform
     * @return
     */
    public int addWxPlatform(WxPlatform wxPlatform) {
        int count = wxPlatformMapper.insertSelective(wxPlatform);
        return count;
    }

    /**
     * 更新公众号
     *
     * @param wxPlatform
     * @return
     */
    public int updateWxPlatform(WxPlatform wxPlatform) {
        int count = wxPlatformMapper.updateByPrimaryKeySelective(wxPlatform);
        return count;
    }

    /**
     * 更新公众号--根据appId
     *
     * @param wxPlatform
     * @return
     */
    public int updateWxPlatformByAppId(WxPlatform wxPlatform) {
        String appId = wxPlatform.getAppId();
        if (StringUtils.isNotBlank(appId)) {
            wxPlatform.setGmtModified(new Date());
            Example example = new Example(WxPlatform.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("appId", appId);
            return wxPlatformMapper.updateByExampleSelective(wxPlatform, example);
        }
        return 0;
    }

    /**
     * 查询状态正常的公众号
     *
     * @return
     */
    public List<WxPlatform> selectWxPlatformsStatusNormal() {
        Example example = new Example(WxPlatform.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("status", PlatformStatusEnum.cancel);
        List<WxPlatform> wxPlatforms = wxPlatformMapper.selectByExample(example);
        return wxPlatforms;
    }

    /**
     * 分页查询--对象转example
     *
     * @param request
     * @return
     */
    public PageInfo<WxPlatform> queryPageListByWhere2Example(WxPlatformPageRequest request) {
        Example example = buildExample4Query(request);
        PageHelper.startPage(request.getPage(), request.getRows());
        List<WxPlatform> wxPlatforms = wxPlatformMapper.selectByExample(example);
        return new PageInfo<>(wxPlatforms);
    }

    /**
     * 组装查询的Example
     *
     * @param request
     * @return
     */
    Example buildExample4Query(WxPlatformPageRequest request) {
        if (Objects.nonNull(request)) {
            Example example = new Example(WxPlatform.class);
            Example.Criteria criteria = example.createCriteria();
            String appId = request.getAppId();
            if (StringUtils.isNotBlank(appId)) {
                criteria.andEqualTo("appId", appId);
            }
            String status = request.getStatus();
            if (StringUtils.isNotBlank(status)) {
                criteria.andEqualTo("status", status);
            }
            String nickName = request.getNickName();
            if (StringUtils.isNotBlank(nickName)) {
                criteria.andLike("nickName", "%" + nickName + "%");
            }
            String serviceTypeInfo = request.getServiceTypeInfo();
            if (StringUtils.isNotBlank(serviceTypeInfo)) {
                criteria.andLike("serviceTypeInfo", "%" + serviceTypeInfo + "%");
            }
            String verifyTypeInfo = request.getVerifyTypeInfo();
            if (StringUtils.isNotBlank(verifyTypeInfo)) {
                criteria.andLike("verifyTypeInfo", "%" + verifyTypeInfo + "%");
            }
            Date startTime = request.getStartTime();
            if (Objects.nonNull(startTime)) {
                criteria.andGreaterThanOrEqualTo("gmtModified", startTime);
            }
            Date endTime = request.getEndTime();
            if (Objects.nonNull(endTime)) {
                criteria.andLessThanOrEqualTo("gmtModified", endTime);
            }
            return example;
        }
        return null;
    }

    /**
     * 查询公众号信息
     *
     * @param appId
     * @return
     */
    public List<WxPlatformDTO> queryWxPlatformByAppId(String appId) {
        Example example = new Example(WxPlatform.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        List<WxPlatform> wxPlatforms = wxPlatformMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(wxPlatforms)) {
            List<WxPlatformDTO> wxPlatformDTOs = BeanConv.copy(wxPlatforms, WxPlatformDTO.class);
            return wxPlatformDTOs;
        }
        return null;
    }

    /**
     * 查询公众号信息
     *
     * @param loginId 登陆帐户
     * @return
     */
    public List<WxPlatformDTO> queryWxPlatformsByLoginUserId(String loginId) {
        List<WxPlatform> wxPlatforms = wxPlatformMapper.selectByLoginId(loginId);
        if (!CollectionUtils.isEmpty(wxPlatforms)) {
            List<WxPlatformDTO> wxPlatformDTOs = BeanConv.copy(wxPlatforms, WxPlatformDTO.class);
            return wxPlatformDTOs;
        }
        return null;
    }

    /**
     * 根据登陆账号分页查询公众号信息
     *
     * @param request
     * @return
     */
    public PageInfo<WxPlatform> queryPageListByLoginUserId(WxPlatformLoginPageRequest request) {
        String loginId = request.getLoginId();
        PageHelper.startPage(request.getPage(), request.getRows());
        WxPlatform wxPlatform = new WxPlatform();
        wxPlatform.setAppId(request.getAppId());
        wxPlatform.setNickName(request.getNickName());
        wxPlatform.setStatus(request.getStatus());
        wxPlatform.setServiceTypeInfo(request.getServiceTypeInfo());
        List<WxPlatform> wxPlatforms = wxPlatformMapper.selectByWhereAndLoginId(loginId, wxPlatform);
        return new PageInfo<>(wxPlatforms);
    }
}
